package com.kamistoat.ruoyi.ruoyicommonredis.configure;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kamistoat.ruoyi.ruoyicommonredis.configure.properties.FastJson2JsonCodec;
import com.kamistoat.ruoyi.ruoyicommonredis.configure.properties.RedissonProperties;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.spring.cache.CacheConfig;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * redisson配置
 * 注意删除了 @EnableCaching，暂时不加入SpringCache
 * 另，为了使用自定义配置，需要按照 配置第三方组件自定义配置 写法来做
 */
@Slf4j
// @EnableCaching
//@EnableConfigurationProperties(RedissonProperties.class)
//@Component
public class RedissonConfig {
// public class RedissonConfig extends CachingConfigurerSupport {
//    @Autowired
//    private RedissonProperties redissonProperties;
    /**
     * 根据配置参数制作 redisson客户端。yaml文件中可以决定是单机还是集群redisson
     *
     * @return
     */
    public static Config fixConfig(RedissonProperties redissonProperties) {
        Config redissonConfig = new Config();
        // redisson通用配置
        redissonConfig.setThreads(redissonProperties.getThreads())
                .setNettyThreads(redissonProperties.getNettyThreads())
                // redisson由于是第三方组件，所以不能像RedisTemplate那样，写一个序列化Bean就能更改序列化机制。
                // redisson必须手动实现一个序列化编解码类，然后在这里指定
                .setCodec(new FastJson2JsonCodec());

        RedissonProperties.SingleServerConfig singleServerConfig = redissonProperties.getSingleServerConfig();
        // redisson单机模式
        if (singleServerConfig != null) {
            redissonConfig.useSingleServer()
                    .setTimeout(singleServerConfig.getTimeout())
                    .setClientName(singleServerConfig.getClientName())
                    .setAddress(singleServerConfig.getAddress())
                    .setIdleConnectionTimeout(singleServerConfig.getIdleConnectionTimeout())
                    .setSubscriptionConnectionPoolSize(singleServerConfig.getSubscriptionConnectionPoolSize())
                    .setConnectionMinimumIdleSize(singleServerConfig.getConnectionMinimumIdleSize())
                    .setConnectionPoolSize(singleServerConfig.getConnectionPoolSize());
        }

        // redisson集群模式
        RedissonProperties.ClusterServersConfig clusterServersConfig = redissonProperties.getClusterServersConfig();
        if (clusterServersConfig != null) {
            redissonConfig.useClusterServers()
                    .setTimeout(clusterServersConfig.getTimeout())
                    .setClientName(clusterServersConfig.getClientName())
                    .setIdleConnectionTimeout(clusterServersConfig.getIdleConnectionTimeout())
                    .setSubscriptionConnectionPoolSize(clusterServersConfig.getSubscriptionConnectionPoolSize())
                    .setMasterConnectionMinimumIdleSize(clusterServersConfig.getMasterConnectionMinimumIdleSize())
                    .setMasterConnectionPoolSize(clusterServersConfig.getMasterConnectionPoolSize())
                    .setSlaveConnectionMinimumIdleSize(clusterServersConfig.getSlaveConnectionMinimumIdleSize())
                    .setSlaveConnectionPoolSize(clusterServersConfig.getSlaveConnectionPoolSize())
                    .setReadMode(clusterServersConfig.getReadMode())
                    .setSubscriptionMode(clusterServersConfig.getSubscriptionMode());
        }
        log.info("初始化 redisson 配置");
        return redissonConfig;
    }

//    @Autowired
//    private ObjectMapper objectMapper;
//    /**
//     * 整合spring-cache
//     */
//    @Bean
//    public CacheManager cacheManager(RedissonClient redissonClient) {
//        List<RedissonProperties.CacheGroup> cacheGroup = redissonProperties.getCacheGroup();
//        Map<String, CacheConfig> config = new HashMap<>();
//        for (RedissonProperties.CacheGroup group : cacheGroup) {
//            CacheConfig cacheConfig = new CacheConfig(group.getTtl(), group.getMaxIdleTime());
//            cacheConfig.setMaxSize(group.getMaxSize());
//            config.put(group.getGroupId(), cacheConfig);
//        }
//        return new RedissonSpringCacheManager(redissonClient, config, new JsonJacksonCodec(objectMapper));
//    }

    /**
     * redis集群配置 yml
     *
     * --- # redis 集群配置(单机与集群只能开启一个另一个需要注释掉)
     * spring:
     *   redis:
     *     cluster:
     *       nodes:
     *         - 192.168.0.100:6379
     *         - 192.168.0.101:6379
     *         - 192.168.0.102:6379
     *     # 密码
     *     password:
     *     # 连接超时时间
     *     timeout: 10s
     *     # 是否开启ssl
     *     ssl: false
     *
     * redisson:
     *   # 线程池数量
     *   threads: 16
     *   # Netty线程池数量
     *   nettyThreads: 32
     *   # 集群配置
     *   clusterServersConfig:
     *     # 客户端名称
     *     clientName: ${ruoyi.name}
     *     # master最小空闲连接数
     *     masterConnectionMinimumIdleSize: 32
     *     # master连接池大小
     *     masterConnectionPoolSize: 64
     *     # slave最小空闲连接数
     *     slaveConnectionMinimumIdleSize: 32
     *     # slave连接池大小
     *     slaveConnectionPoolSize: 64
     *     # 连接空闲超时，单位：毫秒
     *     idleConnectionTimeout: 10000
     *     # 命令等待超时，单位：毫秒
     *     timeout: 3000
     *     # 发布和订阅连接池大小
     *     subscriptionConnectionPoolSize: 50
     *     # 读取模式
     *     readMode: "SLAVE"
     *     # 订阅模式
     *     subscriptionMode: "MASTER"
     */

}
